#!/bin/bash
# 获取当期git分支的tag，如果有v前缀，则取v后面的数字作为版本号，没有符合的tag，结束脚本

# 获取最近一次提交关联的所有标签
tags=$(git tag --points-at HEAD)

# 标志变量，用于标记是否找到以 v 开头的标签
found=false

# 遍历所有标签
for tag in $tags; do
    if [[ $tag == v* ]]; then
        echo "版本号标签: $tag"
        found=true
        break
    fi
done

# 如果没有找到以 v 开头的标签
if [ "$found" = false ]; then
    echo "最近一次提交没有以 v 开头的标签"
    exit 1
fi

ver=${tag:1}

cd ../../

# 当脚本带有 -d 参数时忽略编译打包
if [ "$1" == "-d" ]; then
  echo "ignore build"
else
  mvn clean package -pl oj-web -am
  # exit if build failed
  if [ $? -ne 0 ]; then
    echo "maven build failed"
    exit 1
  fi
fi

cd ./oj-web
cp target/*.jar docker/
cd docker

dos2unix *.sh

image=registry.cn-hangzhou.aliyuncs.com/bookshare-ruiya/oj-backend

# get latest version num
#ver=210

tag=v-$ver

# build and push
docker build -t $image:latest -t $image:$tag .
docker push $image:latest
docker push $image:$tag

rm -rf *.jar

echo 'pushed image to '$image':v-'$ver
